package cn.iocoder.an.module.mold.controller.admin.stockiorecords;

import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.stockiorecords.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.stockiorecords.StockIoRecordsDO;
import cn.iocoder.an.module.mold.service.stockiorecords.StockIoRecordsService;

@Tag(name = "管理后台 - 模具出入库记录")
@RestController
@RequestMapping("/mold/stock-io-records")
@Validated
public class StockIoRecordsController {
    @Resource
    private StockIoRecordsService stockIoRecordsService;

    @PostMapping("/create")
    @Operation(summary = "创建模具出入库记录")
    @PreAuthorize("@ss.hasPermission('mold:stock-io-records:create')")
    public CommonResult<Long> createStockIoRecords(@Valid @RequestBody StockIoRecordsSaveReqVO createReqVO) {
        return success(stockIoRecordsService.createStockIoRecords(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模具出入库记录")
    @PreAuthorize("@ss.hasPermission('mold:stock-io-records:update')")
    public CommonResult<Boolean> updateStockIoRecords(@Valid @RequestBody StockIoRecordsSaveReqVO updateReqVO) {
        stockIoRecordsService.updateStockIoRecords(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模具出入库记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:stock-io-records:delete')")
    public CommonResult<Boolean> deleteStockIoRecords(@RequestParam("id") Long id) {
        stockIoRecordsService.deleteStockIoRecords(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除模具出入库记录")
                @PreAuthorize("@ss.hasPermission('mold:stock-io-records:delete')")
    public CommonResult<Boolean> deleteStockIoRecordsList(@RequestParam("ids") List<Long> ids) {
        stockIoRecordsService.deleteStockIoRecordsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模具出入库记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:stock-io-records:query')")
    public CommonResult<StockIoRecordsRespVO> getStockIoRecords(@RequestParam("id") Long id) {
        StockIoRecordsDO stockIoRecords = stockIoRecordsService.getStockIoRecords(id);
        return success(BeanUtils.toBean(stockIoRecords, StockIoRecordsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模具出入库记录分页")
    @PreAuthorize("@ss.hasPermission('mold:stock-io-records:query')")
    @TransMethodResult
    public CommonResult<PageResult<StockIoRecordsRespVO>> getStockIoRecordsPage(@Valid StockIoRecordsPageReqVO pageReqVO) {
        PageResult<StockIoRecordsDO> pageResult = stockIoRecordsService.getStockIoRecordsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StockIoRecordsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模具出入库记录 Excel")
    @PreAuthorize("@ss.hasPermission('mold:stock-io-records:export')")
    @ApiAccessLog(operateType = EXPORT)
    @TransMethodResult
    public void exportStockIoRecordsExcel(@Valid StockIoRecordsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StockIoRecordsDO> list = stockIoRecordsService.getStockIoRecordsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模具出入库记录.xls", "数据", StockIoRecordsRespVO.class,
                        BeanUtils.toBean(list, StockIoRecordsRespVO.class));
    }

}